package org.delphy.indy2.containers
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	
	import mx.containers.Canvas;
	
	import org.delphy.indy2.containers.carbonPanelClasses.*;
	
	public class CarbonPanel extends Canvas
	{
		
		// Border properties
		
		protected var _border:CarbonBorder;
		public function get border():CarbonBorder{
			return _border;
		}
		
		// Client properties
		
		protected var _content:CarbonPanelContent;
		public function get content():Canvas{
			return _content;
		}
				
		// Resizer
		private var resizerSize:Number;		
		protected var _resizer:Canvas;
		
		public function get resizer():Canvas{
			return _resizer;
		}
		
		protected var _resizing:Boolean=false;
		[Bindable]
		public function get resizing():Boolean{
			return _resizing;
		}
		public function set resizing(value:Boolean):void{
			_resizing=value;
		}
				
		protected var _resizable:Boolean=true;
		public function get resizable():Boolean{
			return _resizable;
		}
		public function set resizable(value:Boolean):void{
			_resizable=value;
			if(_resizable){
				if(_resizer!=null)
					_resizer.addEventListener(MouseEvent.MOUSE_DOWN,startResize);
					_resizer.addEventListener(MouseEvent.MOUSE_OVER,onResizerMouseOver);
					_resizer.addEventListener(MouseEvent.MOUSE_OUT,onResizerMouseOut);

			}
			else{
				if(_resizer!=null)
					_resizer.removeEventListener(MouseEvent.MOUSE_DOWN,startResize);
					_resizer.addEventListener(MouseEvent.MOUSE_OVER,onResizerMouseOver);
					_resizer.addEventListener(MouseEvent.MOUSE_OUT,onResizerMouseOut);
			}
				
		}
		
		// Filters
		private var shadowFilter:DropShadowFilter;
						
		public function CarbonPanel()
		{
			super();
			styleName="carbonPanel";
			_content=new CarbonPanelContent();
			_border=new CarbonBorder();
			_resizer=new Canvas();
			horizontalScrollPolicy="off";
			verticalScrollPolicy="off";
		}
				
		override public function addChild(child:DisplayObject):DisplayObject{
			return _content.addChild(child);
		}
		
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject{
			return _content.addChildAt(child,index);
		}
		
		override public function getChildAt(index:int):DisplayObject{
			return _content.getChildAt(index);
		}
		
		override public function getChildByName(name:String):DisplayObject{
			return _content.getChildByName(name);
		}
		
		override public function getChildIndex(child:DisplayObject):int{
			return _content.getChildIndex(child);
		}
		
		override public function getChildren():Array{
			return _content.getChildren();
		} 
		
		override protected function createChildren():void{
			super.createChildren();
						
			// Add the content and the border to the rawChildren list
			// Note that the order of adding children here is important, since the "border" component is NOT transparent. 
			// If you add the border to the rawChildren first, you'll end up with a non-interactive content 
			rawChildren.addChild(_border);
			rawChildren.addChild(_content);
			rawChildren.addChild(_resizer);
			
			if(_resizable){
				_resizer.addEventListener(MouseEvent.MOUSE_DOWN,startResize);
				_resizer.addEventListener(MouseEvent.MOUSE_OVER,onResizerMouseOver);
				_resizer.addEventListener(MouseEvent.MOUSE_OUT,onResizerMouseOut);
			}			
			
			// Draw filters
			shadowFilter=new DropShadowFilter();			
			filters=[shadowFilter];
		}
		
		protected function startResize(evt:Event):void{
			resizing=true;
			stage.addEventListener(Event.ENTER_FRAME,onResizing);
			stage.addEventListener(MouseEvent.MOUSE_UP,stopResize);
			
			_border.showHilight();
		}
		
		protected function stopResize(evt:Event):void{
			resizing=false;
			stage.removeEventListener(Event.ENTER_FRAME,onResizing);
			stage.removeEventListener(MouseEvent.MOUSE_UP,stopResize);
			
			_border.showHilight();
		}
		
		private function onResizerMouseOver(evt:MouseEvent):void{
			_border.showHilight();
		}
		
		private function onResizerMouseOut(evt:MouseEvent):void{
			if(!resizing)
				_border.hideHilight();
		}
		
		protected function onResizing(evt:Event):void{
			var topLeftPoint:Point=new Point(x,y);
			topLeftPoint=parent.localToGlobal(topLeftPoint);
			
			var newWidth:Number=stage.mouseX-topLeftPoint.x;
			var newHeight:Number=stage.mouseY-topLeftPoint.y;
			newWidth= (newWidth > 0) ? newWidth : 0;
			newHeight= (newHeight >0) ? newHeight : 0;
			
			width=newWidth;
			height=newHeight;
			
			placeResizer();			
		}
		
		public function placeResizer():void{
			_resizer.x=width-resizerSize;
			_resizer.y=height-resizerSize;
		}		
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			
			if(_border!=null){
				_border.styleName=getStyle("borderStyleName");
				_border.width=unscaledWidth;
				_border.height=unscaledHeight;
				_border.alpha=getStyle("borderAlpha");

				resizerSize=_border.cornerRadius;
				resizer.width=resizerSize;
				resizer.height=resizerSize;
				placeResizer();
			}
			
			if(_content!=null){
				_content.styleName=getStyle("contentStyleName");
				_content.width=unscaledWidth - _border.weight *2;
				_content.height=unscaledHeight - _border.weight *2;
				_content.x=_border.weight;
				_content.y=_border.weight;
				_content.alpha=getStyle("contentAlpha");
			}
			
			updateFilters();

		}
		
		private function updateFilters():void{
			shadowFilter.alpha=getStyle("shadowAlpha");
			shadowFilter.blurX=getStyle("shadowSize");
			shadowFilter.blurY=getStyle("shadowSize");
			shadowFilter.distance=getStyle("shadowDistance");
			shadowFilter.angle=getStyle("shadowAngle");
						
			filters=[shadowFilter];			
		}
				
	}
}